<template>
  <el-dialog :title="!dataForm.id ? '新增11' : '修改22'" :close-on-click-modal="false" :visible.sync="visible" width="800px" @close="closeCPDX" append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item label="评票编号" prop="cpCode">
        <el-select v-model="dataForm.cpCode" style="width:90%;">
          <el-option v-for="(item,index) in  cpCodeList " :key="index" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评票名称" prop="cpName">
        <el-input v-model="dataForm.cpName" placeholder="评票名称" style="width:90%;"></el-input>
      </el-form-item>
      <el-form-item label="评票权重" prop="cpQz">
        <el-input type="number" v-model="dataForm.cpQz" placeholder="评票权重" style="width:90%;"></el-input>
      </el-form-item>
      <el-form-item label="是否实名" prop="isSm">
        <el-select :disabled="!dataForm.id ? false : true" v-model="dataForm.isSm" style="width:90%;">
          <el-option v-for="(item,index) in ['实名','匿名']" :key="index" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="评票票数" prop="cpPs">
      <el-input type="number" v-model="dataForm.cpPs" placeholder="评票票数"></el-input>
    </el-form-item> -->
      <!-- <el-form-item label="评票权重（子表合计）" prop="cpQzZx">
      <el-input v-model="dataForm.cpQzZx" placeholder="评票权重"></el-input>
    </el-form-item> -->

    </el-form>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj, getCepingMx } from '@/api/ceping/cepingcpgzcpmxb'

export default {
  data() {
    return {
      cpCodeList: [

      ],
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        gzId: '',
        cpCode: '',
        cpName: '',
        cpQz: '',
        isSm: '',
        cpPs: '',
        cpQzZx: '',
        createTime: '',
        editTime: '',
        createDeptId: '',
        createDeptName: '',
        createUserId: '',
        createUserName: '',
        editDeptId: '',
        editDeptName: '',
        editUserId: '',
        editUserName: '',
        dataState: '',
        tenantId: ''
      },
      dataRule: {
        cpCode: [
          { required: true, message: '评票编号不能为空', trigger: 'blur' }
        ],
        cpName: [
          { required: true, message: '评票名称不能为空', trigger: 'blur' },
          //  { min:2, max:30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        isSm: [
          { required: true, message: '是否实名（实名、匿名）不能为空', trigger: 'blur' }
        ],
        cpPs: [
          { required: true, message: '评票票数不能为空并且只是数字', trigger: 'blur' }
        ],
             cpQz: [
          { required: true, message: '评票权重不能为空并且只是数字', trigger: 'blur' }
        ],

      }
    }
  },

  methods: {
    init(gzId, mxId) {
      this.cpCodeList = [];
      this.loadCode();
      this.dataForm.id = mxId || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
       //console.log('来这了')
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data
          })
        } else {
          this.restPropertes();
          this.dataForm.gzId = gzId;
        }
      })
      this.removeTargetCode(gzId);

    },
    restPropertes(){
        this.dataForm={
        id: 0,
        gzId: '',
        cpCode: '',
        cpName: '',
        cpQz: '',
        isSm: '',
        cpPs: '',
        cpQzZx: '',
        createTime: '',
        editTime: '',
        createDeptId: '',
        createDeptName: '',
        createUserId: '',
        createUserName: '',
        editDeptId: '',
        editDeptName: '',
        editUserId: '',
        editUserName: '',
        dataState: '',
        tenantId: ''
      }
    },
    //移除已经存在的code
    removeTargetCode(gzId) {
      this.$nextTick(() => {
        getCepingMx(gzId).then(response => {
          var obj = response.data.data;
          for (var i = 0; i < obj.length; i++) {
            for (var j = 0; j < this.cpCodeList.length; j++) {
              if (this.cpCodeList[j] == obj[i].cpCode) {
                this.cpCodeList.splice(j, 1)
              }
            }
          }
        });
      });
    },
    //加载code
    loadCode() {
      this.cpCodeList = ['A','A1','A2','A3','A4','A5','A6','A7','A8','A9','B','B1','B2','B3','B4','B5','B6','B7','B8','B9','C','C1','C2','C3','D','D1','D2','D3','E','E1','E2','E3','F','F1','F2','F3','G','G1','G2','G3'];

      // for (var i = 65; i < 91; i++) {
      //   this.cpCodeList.push(String.fromCharCode(i))
      // }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refGzmxList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refGzmxList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //关闭测评对象
    closeCPDX(){
      this.$emit('closeCPDX')
    }
  }
}
</script>
